import { Injectable } from '@angular/core';
import {CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree} from '@angular/router';

/**
 * 路由守卫：专门用于登陆界面的强制跳转
 * */
@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {

  constructor(private router: Router,
              ) { }

  canActivate(route: ActivatedRouteSnapshot,
              state: RouterStateSnapshot): boolean {
    const token = localStorage.getItem("token");
    if(token) return true;
    else{
      this.router.navigate(['/login'], {
        queryParams: {returnUrl: state.url}
      }).then();
      return false;
    }
  }
}
